<template>
  <el-dialog :title="dataForm.name" :close-on-click-modal="false" :visible.sync="visible" width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <!-- <el-form-item v-if="cktype" label="序号" prop="numId">
				<el-input size="small" v-model="dataForm.numId" placeholder="" disabled></el-input>
			</el-form-item> -->
      <el-form-item label="角色名称" prop="roleName">
        <el-input size="small" v-model="dataForm.roleName" placeholder="角色名称" style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="角色说明" prop="roleRemark">
        <el-input size="small" rows="3" v-model="dataForm.roleRemark" type="textarea" style="width:80%" :autosize="{ minRows: 3, maxRows: 4}"
          placeholder="角色说明"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="dataFormSubmit()">确定</el-button>

      </el-form-item>

    </el-form>

  </el-dialog>
</template>

<script>
  // 	import {
  // 		treeDataTranslate
  // 	} from '@/utils'
  import {
    addRole,
    updataRole
  } from '../../api/src/system/role'
  export default {
    name: 'roleAddUpdata',
    data() {
      return {
        visible: false,
        menuList: [],
        cktype: true, // 判断是增加还是修改
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        },
        dataForm: {
          numId: '',
          name: '',
          roleId: '',
          roleName: '',
          roleDataList: [''],
          roleRemark: ''
        },
        dataRule: {
          roleId: [{
            required: true,
            message: '序号不能为空',
            trigger: 'blur'
          }],
          roleName: [{
            required: true,
            message: '角色名称不能为空',
            trigger: 'blur'
          }],
          roleRemark: [{
            required: true,
            message: '角色说明不能为空',
            trigger: 'blur'
          }]
        },

      }
    },
    mounted() {

    },
    methods: {

      init(row, index) {
        console.log('获取到的', index)
        if (row == undefined) {
          this.dataForm.name = '新增'
          this.dataForm.numId = ''
          this.dataForm.roleId = ''
          this.dataForm.roleName = ''
          this.dataForm.roleRemark = ''
          this.dataForm.roleDataList = ['']
          this.cktype = false
        } else {
          this.dataForm.name = '修改'
          this.dataForm.roleId = row.id
          this.dataForm.roleName = row.name
          this.dataForm.roleRemark = row.remark
          this.dataForm.roleDataList = row.authData
          this.dataForm.numId = index

          this.cktype = true
          console.log('修改后的6666', row.authData)
        }

        console.log('修改后的', this.dataForm.numId)

        this.visible = true

      },
      // 表单提交
      async dataFormSubmit() {
        if (this.cktype == true) {
          // 修改
          console.log('di的数数据:', this.dataForm.roleDataList)
          var obj = JSON.parse(this.dataForm.roleDataList)
          var authList = obj.authList
          console.log('di的数数据6999:', authList)
          await updataRole(authList, this.dataForm.roleId, this.dataForm.roleName, this.dataForm.roleRemark).then(res => {
            console.log('修改成功::::::::>>>>', res)


          })
          setTimeout(this.$emit('getDataList'), 800)
        } else {
          // 增加
          await addRole(this.dataForm.roleName, this.dataForm.roleRemark).then(res => {
            console.log('增加成功::::::::>>>>', res)


          })
          setTimeout(this.$emit('getDataList'), 800)
        }



        this.visible = false

      }
    }
  }
</script>
